<template>
  <div style="text-align: center; width: 680px; margin: auto; background: #fff">
    <table
        cellpadding="0"
        cellspacing="0"
        style="
        table-layout: fixed;
        position: relative;
        width: 95%;
        font-family: 仿宋_GB2312;
        font-size: 16px;
        margin: auto;
      "
    >
      <tr style="height: 10px">
        <td
            v-for="item in 100"
            :key="item"
            style="width: 1%; text-align: center"
        />
      </tr>
      <tr style="height: 60px">
        <td colspan="100" style="text-align: center">
          <span id="Lab_title" style="font-size: 24px; font-weight: bold"
          >水利水电工程施工企业安全生产管理人员<br/>考核合格证书个人注销申请表</span
          >
        </td>
      </tr>
      <tr style="height: 50px">
        <td colspan="14" class="TableBorderOther">姓名</td>
        <td colspan="28" class="TableBorderOther">
          {{ formData.name }}
        </td>
        <td colspan="14" class="TableBorderOther">性别</td>
        <td colspan="28" class="TableBorderOther">
          <Aselect
              :disabled="disabled || sexCode"
              v-model="formData.sex"
              :options="$dict('sex').list()"
              placeholder=""
          />
        </td>
        <td colspan="16" rowspan="4" class="TableBorderRight">
          <img
              style="width: 100%; height: 100%"
              :src="tempFormData.idPhoto && tempFormData.idPhoto.thumbnailUri"
              alt=""
          />
        </td>
      </tr>
      <tr style="height: 50px">
        <td colspan="14" class="TableBorderOther">身份证号</td>
        <td colspan="70" class="TableBorderOther">
          <input
              :disabled="disabled || idCode"
              v-model.trim="tempFormData.paperCode"
              type="text"
              class="input-center"
          />
        </td>
      </tr>
      <tr style="height: 50px">
        <td colspan="20" class="TableBorderOther">管理人员类别</td>
        <td colspan="64" class="TableBorderOther">
          <Aselect
              :disabled="disabled"
              v-model="tempFormData.safeTypeCode"
              :options="safeTypeDict"
              @change="safeTypeChange"
              placeholder=""
          />
        </td>

      </tr>
      <tr style="height: 50px">
        <td colspan="14" class="TableBorderOther">证书编号</td>
        <td colspan="40" class="TableBorderOther">
          <input
              :disabled="disabled || autoInput"
              v-model.trim="tempFormData.certNumCode"
              type="text"
              class="input-center" />
        </td>
        <td colspan="14" class="TableBorderOther">发证日期</td>
        <td colspan="16" class="TableBorderOther">
          <AdatePicker
              :disabled="disabled || autoInput"
              v-model="tempFormData.issuDate"
              placeholder=""
          />
        </td>
      </tr>
      <tr style="height: 50px">
        <td colspan="14" :class="checkValue1 === '3' ? 'TableBorderOther' : 'TableBorderBottom'">注销原因</td>
        <td colspan="86" :class="checkValue1 === '3' ? 'TableBorderRight' : 'TableBorderRightBottom'">
          <span><input
              :checked="checkValue1 === '1'"
              @click="checkValue1 = checkValue1 ==='1' ? undefined : '1'"
              type="checkbox"
              style="margin-right: 10px"
              :disabled="disabled"
          />岗位调整</span>
          <span style="margin-left: 16px"><input
              :checked="checkValue1 === '2'"
              @click="checkValue1 = checkValue1 === '2' ? undefined : '2'"
              type="checkbox"
              style="margin-right: 10px"
              :disabled="disabled"
          />解聘</span>
          <span style="margin-left: 16px"><input
              :checked="checkValue1 === '3'"
              @click="checkValue1 = checkValue1 === '3' ? undefined : '3'"
              type="checkbox"
              style="margin-right: 10px"
              :disabled="disabled"
          />其他</span>
        </td>
      </tr>
      <tr v-if="checkValue1 === '3'" style="height: 50px">
        <td colspan="14" class="TableBorderBottom">原因</td>
        <td colspan="86" class="TableBorderRightBottom">
          <input
              :disabled="disabled"
              v-model.trim="tempFormData.delReason"
              type="text"
              class="input-center"
          />
        </td>
      </tr>
    </table>
  </div>
</template>
<script>
import Aselect from '@/components/Select/Aselect';
import { fieldsValidate } from '@/utils/validate.js';
import safeTypeDict from '@/assets/js/safeTypeDict.js';
import businessApi from '@/api/business';
export default {
  props: {
    formData: {
      type: Object,
    },
    disabled: {
      type: Boolean,
      default: false,
    },
  },
  components: { Aselect },
  data() {
    return {
      dialogVisible: false,
      checkValue1: undefined,
      idCode: '',
      sexCode: '',
      tempFormData: {},
      autoInput: true,
      safeTypeDict: safeTypeDict.safeType,
    };
  },
  watch: {
    formData: {
      handler(val) {
        if (val.delReason === '岗位调整') {
          this.checkValue1 = '1';
        } else if (val.delReason === '解聘') {
          this.checkValue1 = '2';
        } else if (val.delReason) {
          this.checkValue1 = '3';
        } else {
          this.checkValue1 = undefined;
        }
        this.tempFormData = val || {};
        if (this.tempFormData.paperType === '111') {
          this.idCode = this.tempFormData.paperCode;
          this.sexCode = val?.sex
        }
      },
      immediate: true,
    },
    checkValue1: {
      handler(val) {
        if (val === '1') {
          this.tempFormData.delReason = '岗位调整';
        } else if (val === '2') {
          this.tempFormData.delReason = '解聘';
        } else if (val === '3'){
          this.tempFormData.delReason = this.formData.delReason || '';
        }
      },
      immediate: true,
    },
  },
  methods: {
    // 安管人员类型变更
    safeTypeChange(val) {
      const params = {
        type: 'd',
        userId: this.userId || this.$store.state.user.userInfo.id,
        safeTypeCode: val,
      }
      businessApi.getOldCertInfo(params).then((res) => {
        const mwrCert = businessApi.getCertInfoFromMwr(val);
        if (res) {
          this.tempFormData = Object.assign({}, this.tempFormData, res, this.formData);
          this.tempFormData.safeTypeCode = val;
        }
        if (mwrCert) {
          this.tempFormData = Object.assign({},
              businessApi.ignoreEmpty(this.tempFormData),
              businessApi.ignoreEmpty(mwrCert));
          this.tempFormData.safeTypeCode = val;
        }
        if (!res && !mwrCert) {
          this.autoInput = false;
            // this.$message.error('未查询到该安管人员的原证书信息');
            // this.tempFormData.safeTypeCode = undefined;
        }
      });

    },
    validateFields() {
      const rules = {
        delReason: [
          { required: true, text: '请输入注销原因' },
          {
            maxLength: 100,
            text: '注销原因不应超过100位',
          }
        ],
        certNumCode: [
          { required: true, text: '选择注销管理人员类别' },
        ],
        safeTypeCode: [
          { required: true, text: '选择注销管理人员类别' },
        ],
        issuDate: [
          { required: true, text: '请选择发证日期' },
        ],
      };
      return fieldsValidate(this.tempFormData, rules, this);
    },
    validateForm() {
      const idReg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
      if (
          this.formData.paperType === '111' &&
          !idReg.test(this.formData.paperCode)
      ) {
        this.$message.error('请输入正确的身份证号码');
        return false;
      }
      return this.validateFields();
    },
  },
};
</script>
<style lang="scss" scoped>
@import '../../../../../../../assets/styles/word.scss';
</style>
